package com.maxwell.googleplay.fragment;

import java.util.List;
import java.util.Random;

import com.maxwell.googleplay.base.BaseFragment;
import com.maxwell.googleplay.base.LoadingPager.LoadedResult;
import com.maxwell.googleplay.base.SuperBaseAdapter;
import com.maxwell.googleplay.bean.CategoryInfoBean;
import com.maxwell.googleplay.factory.ListViewFactory;
import com.maxwell.googleplay.holder.BaseHolder;
import com.maxwell.googleplay.holder.CategoryItemHolder;
import com.maxwell.googleplay.holder.CategoryTitleHolder;
import com.maxwell.googleplay.protocol.CategoryProtocol;
import com.maxwell.googleplay.utils.UIUtils;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * @author yue
 * @创建时间 2016-3-30上午9:26:27
 * @描述 TODO
 * 
 */
public class CategoryFragment extends BaseFragment {
	
	
	private List<CategoryInfoBean> mDatas;

	@Override
	public LoadedResult initData() {
		try {
			CategoryProtocol protocol = new CategoryProtocol();
			mDatas = protocol.loadData(0);
			checkState(mDatas);
			return LoadedResult.SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return LoadedResult.ERROR;
			
		}
		
	}

	@Override
	public View initSuccessView() {
		// 
		ListView listView = ListViewFactory.createListView();
		
		
		listView.setAdapter(new CategoryAdapter(listView, mDatas));
		
		
		
		
		return listView;
	}
	
	class CategoryAdapter extends SuperBaseAdapter<CategoryInfoBean>{

		public CategoryAdapter(AbsListView absListView,
				List<CategoryInfoBean> dataSource) {
			super(absListView, dataSource);
			// TODO Auto-generated constructor stub
		}

		

		@Override
		public BaseHolder<CategoryInfoBean> getSpecialHolder(int position) {
			
			CategoryInfoBean categoryInfoBean = mDatas.get(position);
					
			//如果是title，返回titleHolder
			//如果不是title，返回itemHolder
			if(categoryInfoBean.isTitle){
				return new CategoryTitleHolder();
			}else{
				return new CategoryItemHolder();
			}
		}
		
		@Override
		public int getViewTypeCount() {
			// TODO Auto-generated method stub
			return super.getViewTypeCount() + 1;
		}
	@Override
	public int getNormalType(int position) {
		CategoryInfoBean categoryInfoBean = mDatas.get(position);
		if(categoryInfoBean.isTitle){
			return super.getNormalType(position) + 1;
		}else{
			return super.getNormalType(position);
		}
		
		
		
	}
		
	}
}
